package cn.lzd.mr.case9;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lzd on 2019/3/26.
 */
public class IdsBean implements WritableComparable<IdsBean> {

    private String companyId;
    private String companyName;
    private String ids;

    public IdsBean() {
    }

    public IdsBean(String companyId, String companyName, String ids) {
        this.companyId = companyId;
        this.companyName = companyName;
        this.ids = ids;
    }

    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public void setIds(String ids) {
        this.ids = ids;
    }

    public String getCompanyId() {
        return companyId;
    }

    public String getCompanyName() {
        return companyName;
    }

    public String getIds() {
        return ids;
    }

    public List<String> ids2List() {
        JSONObject json = JSONObject.parseObject(ids);
        List<String> list = new ArrayList<>();
        for (String key : json.keySet()) {
            JSONArray jsonArray = json.getJSONArray(key);
            for (Object o : jsonArray) {
                list.add(key + "=" + o.toString());
            }
        }
        return list;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(this.companyId);
        dataOutput.writeUTF(this.companyName);
        dataOutput.writeUTF(this.ids);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.companyId = dataInput.readUTF();
        this.companyName = dataInput.readUTF();
        this.ids = dataInput.readUTF();
    }

    @Override
    public int compareTo(IdsBean o) {
        return o.getCompanyId().compareTo(this.companyId);
    }
}
